<?php

/**
 * @Created by PhpStorm.
 * @Name 交易市场
 * @User: yu
 * @Date: 2016/11/7
 * @Copyright : MPE.Fiidee.Monda.Cn
 */

namespace trading\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use common\action\FrontCommonAction;

class ProductAction extends FrontCommonAction
{
    protected $serviceBean = "product.product.service";


    public function index(HttpRequest $request)
    {

        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 15;
        $this->page = $page;
        $this->pagesize = $pagesize;

        $productService = Beans::get($this->serviceBean);

        $search_arr = array(

            "kw" => urldecode($request->getParameter('kw')),//关键词
            "delivery_place" => urldecode($request->getParameter('delivery_place')),//交收地
            "delivery_way" => urldecode($request->getParameter('delivery_way')),//交收方式
            "type_name" => urldecode($request->getParameter('type_name')),
            "product_name" => urldecode($request->getParameter('product_name')),
            "factory_name" => urldecode($request->getParameter('factory_name')),
            "brand_name" => urldecode($request->getParameter('brand_name')),
            "repertory_city_name" => urldecode($request->getParameter('repertory_city_name')),
            "order_name" => urldecode($request->getParameter('order_name')),
            "order_way" => urldecode($request->getParameter('order_way')),

            "start_price" => $request->getParameter('start_price'),
            "end_price" => $request->getParameter('end_price'),
            "member_id" => $this->_user->id,

        );
        $arr = $productService->search($pagesize, $page, $search_arr);
        $items = $arr['items'];
        foreach ($items as $k => $item) {

            $items[$k]['delivery_way'] = $productService->getDeliveryWayText($item['delivery_way']);
            $items[$k]['publish_way'] = $productService->getPublishWayText($item['publish_way']);
            $items[$k]['status'] = $productService->getStatusText($item['status']);


        }
        $search_arr['delivery_way_name'] = $productService->getDeliveryWayText($search_arr['delivery_way']);
        $this->getPagemenu($arr['total']);
        $this->getSimplePagemenu($arr['total']);


        $myTPFBArray = $productService->handleMyTPFBArray(true);
        $this->assign('items', $items);
        $this->assign('search_arr', $search_arr);
        $this->assign('myTPFBArray', $myTPFBArray);


        $this->assign("title", "交易市场");
        $this->assign('bread', array('交易市场', $this->getTemplateVar('title')));
        $this->assign('breadUrl', array(url('admin/product/index'), 'javascript:;'));
        $this->setView('product_index');

    }

    /**
     * 加入购物车
     * @param HttpRequest $request
     */
    public function addCartProduct(HttpRequest $request)
    {
        $id = $request->getParameter('id');
        $cartService = Beans::get('trading.tradingCart.service');
        $member_id = $this->_user->__get('id');

        if(!$id){
            AjaxResult::ajaxResult('001', '没有选择产品');
        }
        if($this->_user->hasLogin()){
            $res = $cartService->addCartProduct($member_id, $id);
            $item = $cartService->getCarts($member_id);
            $productIdArr = $item['productIdArr']?$item['productIdArr']:array();
            setCookie('cartInfo',json_encode($productIdArr),time()+3600*24*365,'/');
        }else{
            $productIdArr = json_decode($_COOKIE['cartInfo']);
            if(in_array($id,$productIdArr)){
                $res = -2;
            }else{
                $productIdArr[] = $id;
                setCookie('cartInfo',json_encode($productIdArr),time()+3600*24*365,'/');
                $res = 1;
            }
        }
        if($res == -1){
            AjaxResult::ajaxResult('001','产品信息有误');
        }elseif($res == -2){
            AjaxResult::ajaxResult('001','购物车已经存在此产品');
        }elseif($res == false){
            AjaxResult::ajaxFailtureResult();
        }else{
            AjaxResult::ajaxSuccessResult();
        }
    }

    /**
     * 获取产品信息
     * @param HttpRequest $request
     */
    public function getProduct(HttpRequest $request)
    {
        $productService = Beans::get($this->serviceBean);

        $id = $request->getParameter('id');
        $member_id = $this->_user->__get('id');

        if (!$member_id) {
            AjaxResult::ajaxResult('001', '您还没登录，不能进行此项操作，请先登录或者注册');
        }
        $item = $productService->getProduct($id, $member_id);

        $item['delivery_way'] = $productService->getDeliveryWayText($item['delivery_way']);
        $item['quantity']=$item['quantity']-$item['freeze_quantity'];

        if ($item) {
            AjaxResult::ajaxResult('000', 'ok', $item);
        } else {
            AjaxResult::ajaxResult('001', 'no', '');
        }
    }


}